package com.lby.springcloud.ddd.usercontext.north.remote.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.client.utils.StringUtils;
import com.lby.springcloud.ddd.usercontext.north.local.UserAppService;
import com.lby.springcloud.ddd.usercontext.pl.request.LoginReq;
import com.lby.springcloud.ddd.usercontext.pl.response.Response;

@RestController
public class UserController {

	@Autowired
	private UserAppService userAppService;
	
    @PostMapping("/login")
    public Response<LoginReq> login(@RequestBody LoginReq loginReq) throws Exception {
    	assertNull(loginReq);
    	String account = loginReq.getAccount();
    	String passport = loginReq.getPassport();
    	boolean b = userAppService.login(account, passport);
    	Response<LoginReq> response = new Response<LoginReq>();
    	if(b) {
    		response.setCode(Response.SUCCESS);
    		response.setData(loginReq);
    		response.setMsg("loginSuccess!");
    	} else {
    		response.setCode(Response.SUCCESS);
    		response.setMsg("can not find user by passport");
    	}
    	return response;
    }

	private void assertNull(LoginReq loginReq) throws Exception {
		String account = loginReq.getAccount();
    	String passport = loginReq.getPassport();
		if(StringUtils.isBlank(account) || StringUtils.isBlank(passport)) {
			throw new Exception("account or passport can be null");
		}
	}
}
